#!/usr/bin/env bash
# db-load -- Loads file to the PostgreSQL database configured for a DeepDive application
# > db-load TABLE COLUMNS FORMAT [SOURCE...]
##
set -eu

[[ $# -gt 0 ]] || usage "$0" "No TABLE given"
table=$1; shift
[[ $# -gt 0 ]] || usage "$0" "No COLUMNS given"
columns=$1; shift
[[ $# -gt 0 ]] || usage "$0" "No FORMAT given"
format=$1; shift

table="$table${columns:+($columns)}"

# load in parallel from multiple sources
do_load() {
    local sql=$1; shift
    local mkloadingexpr=$1; shift
    local nsources=$#
    if [[ $nsources -gt 1 ]]; then
        local i=1
        for source; do
            eval 'show_progress input_to "loading $table [$i/$nsources]" -- \
                db-execute "$sql" <'"$("$mkloadingexpr" "$source")"' &'
            let ++i
        done
        wait
    else
        source=$1; shift
        eval 'exec show_progress input_to "loading $table" -- \
            db-execute "$sql" <'"$("$mkloadingexpr" "$source")"''
    fi
}

copy_option=
mkloadingexpr=escape4sh
case $format in
    tsv)
        ;;
    tsj)
        copy_option+=" CSV"  # CSV is better for distinguishing NULLs from empty strings
        mkloadingexpr=primitive_type_handlers
        primitive_type_handlers() {
            local s=$1; shift
            echo " <($(eval escape4sh exec tsj2fmt csv "$(
                db-execute "$(IFS+=,; sql_for_column_types "$table" $columns)" |
                tr a-z A-Z | escape4sh
            )") <$(escape4sh "$s"))"
        }
        ;;
    csv)
        copy_option+="CSV"
        ;;
    *) error "$format: unsupported format by PostgreSQL driver" ;;
esac

do_load "\COPY $table FROM PSTDIN${copy_option:+ $copy_option}" "$mkloadingexpr" "$@"
